<!-- src/components/TeacherDashboard.vue -->
<template>
  <div class="teacher-dashboard">
    <!-- 欢迎信息 -->
    <div class="welcome-section">
      <div class="welcome-content">
        <h2>欢迎回来，{{ teacherName }}老师！</h2>
        <p>今天是 {{ currentDate }}，您有 {{ todayClasses }} 节课程安排</p>
      </div>
      <div class="quick-actions">
        <button class="action-btn primary" @click="createCourse">
          <i class="icon-plus"></i>
          创建课程
        </button>
        <button class="action-btn secondary" @click="viewSchedule">
          <i class="icon-calendar"></i>
          查看课表
        </button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon courses">
          <i class="icon-book"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.myCourses || 0 }}</div>
          <div class="stat-label">我的课程</div>
          <div class="stat-change positive">+2 本月</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon students">
          <i class="icon-users"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.myStudents || 0 }}</div>
          <div class="stat-label">我的学员</div>
          <div class="stat-change positive">+15 本月</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon sessions">
          <i class="icon-calendar"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.totalSessions || 0 }}</div>
          <div class="stat-label">授课场次</div>
          <div class="stat-change positive">+8 本月</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon rating">
          <i class="icon-star"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.rating || 4.8 }}</div>
          <div class="stat-label">教学评分</div>
          <div class="stat-change positive">+0.2 本月</div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="dashboard-content">
      <!-- 今日课程 -->
      <div class="content-card">
        <div class="card-header">
          <h3>今日课程</h3>
          <router-link to="/teacher/schedule" class="view-all">查看全部</router-link>
        </div>
        <div class="today-classes">
          <div v-if="todayClassesList.length === 0" class="empty-state">
            <i class="icon-calendar-empty"></i>
            <p>今天暂无课程安排</p>
          </div>
          <div v-else class="class-list">
            <div v-for="classItem in todayClassesList" :key="classItem.id" class="class-item">
              <div class="class-time">
                <span class="time">{{ classItem.time }}</span>
                <span class="duration">{{ classItem.duration }}分钟</span>
              </div>
              <div class="class-info">
                <h4>{{ classItem.courseName }}</h4>
                <p>{{ classItem.location }} · {{ classItem.studentCount }}人</p>
              </div>
              <div class="class-status">
                <span :class="['status', classItem.status]">{{ getStatusText(classItem.status) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 最近学员 -->
      <div class="content-card">
        <div class="card-header">
          <h3>最近学员</h3>
          <router-link to="/teacher/students" class="view-all">查看全部</router-link>
        </div>
        <div class="recent-students">
          <div v-if="recentStudents.length === 0" class="empty-state">
            <i class="icon-users-empty"></i>
            <p>暂无学员数据</p>
          </div>
          <div v-else class="student-list">
            <div v-for="student in recentStudents" :key="student.id" class="student-item">
              <img :src="student.avatar" :alt="student.name" class="student-avatar" />
              <div class="student-info">
                <h4>{{ student.name }}</h4>
                <p>{{ student.course }} · 进度 {{ student.progress }}%</p>
              </div>
              <div class="student-actions">
                <button class="btn-icon" @click="contactStudent(student.id)">
                  <i class="icon-message"></i>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 教学分析 -->
    <div class="analytics-section">
      <div class="content-card full-width">
        <div class="card-header">
          <h3>教学分析</h3>
          <div class="chart-controls">
            <select v-model="chartPeriod" @change="updateChart">
              <option value="week">本周</option>
              <option value="month">本月</option>
              <option value="quarter">本季度</option>
            </select>
          </div>
        </div>
        <div class="chart-container">
          <div class="chart-placeholder">
            <p>教学数据图表区域</p>
            <p>可显示：授课时长、学员参与度、课程完成率等</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

// 响应式数据
const statistics = ref({
  myCourses: 0,
  myStudents: 0,
  totalSessions: 0,
  rating: 4.8
})

const todayClassesList = ref([
  {
    id: 1,
    time: '09:00',
    duration: 120,
    courseName: '农业种植技术基础',
    location: '教室A101',
    studentCount: 25,
    status: 'upcoming'
  },
  {
    id: 2,
    time: '14:00',
    duration: 90,
    courseName: '现代农机操作',
    location: '实训基地',
    studentCount: 18,
    status: 'upcoming'
  }
])

const recentStudents = ref([
  {
    id: 1,
    name: '张三',
    avatar: '/default-student-avatar.png',
    course: '农业种植技术',
    progress: 75
  },
  {
    id: 2,
    name: '李四',
    avatar: '/default-student-avatar.png',
    course: '现代农机操作',
    progress: 60
  }
])

const chartPeriod = ref('week')

// 计算属性
const teacherName = computed(() => {
  return authStore.userInfo?.name || '老师'
})

const currentDate = computed(() => {
  return new Date().toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  })
})

const todayClasses = computed(() => {
  return todayClassesList.value.length
})

// 方法
const getStatusText = (status) => {
  const statusMap = {
    upcoming: '即将开始',
    ongoing: '进行中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知'
}

const createCourse = () => {
  router.push('/teacher/courses?action=create')
}

const viewSchedule = () => {
  router.push('/teacher/schedule')
}

const contactStudent = (studentId) => {
  alert(`联系学员功能待开发 - 学员ID: ${studentId}`)
}

const updateChart = () => {
  console.log('更新图表:', chartPeriod.value)
}

const fetchTeacherData = async () => {
  try {
    // 这里调用API获取教师相关数据
    console.log('获取教师数据')
  } catch (error) {
    console.error('获取教师数据失败:', error)
  }
}

onMounted(() => {
  fetchTeacherData()
})
</script>

<style scoped>
.teacher-dashboard {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-section {
  background: linear-gradient(135deg, #10B981 0%, #059669 100%);
  color: white;
  padding: 24px;
  border-radius: 12px;
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-content h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
}

.welcome-content p {
  margin: 0;
  opacity: 0.9;
}

.quick-actions {
  display: flex;
  gap: 12px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn.primary {
  background: white;
  color: #10B981;
}

.action-btn.secondary {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255,255, 255, 0.3);
}

.action-btn:hover {
  transform: translateY(-2px);
}

.icon-plus::before {
  content: "➕";
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
}

.stat-icon.courses { background: #10B981; }
.stat-icon.students { background: #3B82F6; }
.stat-icon.sessions { background: #F59E0B; }
.stat-icon.rating { background: #EF4444; }

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #1F2937;
  margin-bottom: 4px;
}

.stat-label {
  color: #6B7280;
  font-size: 14px;
  margin-bottom: 4px;
}

.stat-change {
  font-size: 12px;
  font-weight: 500;
}

.stat-change.positive {
  color: #10B981;
}

.icon-star::before {
  content: "⭐";
}

.dashboard-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 24px;
}

.content-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
}

.content-card.full-width {
  grid-column: 1 / -1;
}

.card-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid #E5E7EB;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  color: #1F2937;
  font-size: 18px;
}

.view-all {
  color: #10B981;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
}

.view-all:hover {
  text-decoration: underline;
}

.today-classes,
.recent-students {
  padding: 0 24px 24px;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #6B7280;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  display: block;
}

.icon-calendar-empty::before {
  content: "📅";
}

.icon-users-empty::before {
  content: "👥";
}

.class-list,
.student-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.class-item,
.student-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: #F9FAFB;
  border-radius: 8px;
}

.class-time {
  min-width: 80px;
  text-align: center;
}

.class-time .time {
  display: block;
  font-size: 18px;
  font-weight: 600;
  color: #1F2937;
}

.class-time .duration {
  font-size: 12px;
  color: #6B7280;
}

.class-info,
.student-info {
  flex: 1;
}

.class-info h4,
.student-info h4 {
  margin: 0 0 4px 0;
  color: #1F2937;
  font-size: 16px;
}

.class-info p,
.student-info p {
  margin: 0;
  color: #6B7280;
  font-size: 14px;
}

.class-status .status {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.status.upcoming {
  background: #FEF3C7;
  color: #D97706;
}

.status.ongoing {
  background: #D1FAE5;
  color: #059669;
}

.status.completed {
  background: #E0E7FF;
  color: #3730A3;
}

.student-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #E5E7EB;
}

.student-actions {
  display: flex;
  gap: 8px;
}

.btn-icon {
  width: 36px;
  height: 36px;
  border: none;
  background: #F3F4F6;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-icon:hover {
  background: #E5E7EB;
}

.icon-message::before {
  content: "💬";
}

.analytics-section {
  margin-bottom: 24px;
}

.chart-controls {
  display: flex;
  gap: 12px;
}

.chart-controls select {
  padding: 8px 12px;
  border: 1px solid #D1D5DB;
  border-radius: 6px;
  font-size: 14px;
}

.chart-container {
  padding: 24px;
}

.chart-placeholder {
  height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #F9FAFB;
  border-radius: 8px;
  color: #6B7280;
  text-align: center;
}

@media (max-width: 768px) {
  .welcome-section {
    flex-direction: column;
    text-align: center;
    gap: 16px;
  }
  
  .dashboard-content {
    grid-template-columns: 1fr;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
}
</style>